package com.angel.components.imprint.impl
{

	
	import com.angel.components.imprint.Printer;
	
	import mx.core.Application;
	import mx.core.UIComponent;
	import mx.printing.FlexPrintJob;
	
	public class UIComponentPrinter implements Printer
	{
//		[Bindable]
//		public var headerBuilder:UIComponentPrintableBuilder;
//		[Bindable]
//		public var bodyBuilder:UIComponentPrintableBuilder;
//		[Bindable]
//		public var footerBuilder:UIComponentPrintableBuilder;
		[Bindable]
		private var printJob:FlexPrintJob;
	
		public function UIComponentPrinter(_flexPrintJob:FlexPrintJob = null){
			if(_flexPrintJob == null){
				_flexPrintJob = new FlexPrintJob();
			}
			this.printJob = _flexPrintJob;
		}
		
		
//		public function addBodyBuilder(_bodyBuilder:UIComponentPrintableBuilder):void{
//			this.bodyBuilder = _bodyBuilder;
//		}
//
//		public function addHeaderBuilder(_headerBuilder:UIComponentPrintableBuilder):void{
//			this.headerBuilder = _headerBuilder;
//		}
//
//		public function addFooterBuilder(_footerBuilder:UIComponentPrintableBuilder):void{
//			this.footerBuilder = _footerBuilder;
//		}
		
		public function print(uiComponent:UIComponent):void {
            if (printJob.start()) {
            	uiComponent.setStyle("background-color","white");
//            	if(headerBuilder != null){
//            		header = headerBuilder.buildUIComponent(object);
//            	}
//				if(bodyBuilder != null){
//            		body = bodyBuilder.buildUIComponent(object);
//    			}
//    			if(footerBuilder != null){
//            		footer = footerBuilder.buildUIComponent(object);
//       			}
                
//                Application.application.setStyle(" background-color","#FFFFFF");
                //Seteo las propiedades de la vista de impresion.
//				if(header != null){
//					header.visible = true;
//					Application.application.addChild(header);
//					printJob.addObject(header);
//					header.width = printJob.pageWidth;
//                	header.height = printJob.pageHeight;
//				}
				if(uiComponent != null){
					Application.application.addChild(uiComponent);
					printJob.addObject(uiComponent);
//                	uiComponent.percentWidth = 80;
                	Application.application.percentWidth = 90;
                	Application.application.setStyle("background-color","white");
                	var backgroundColor:String = Application.application.getStyle("backgroundc-color").toString();
//					object.width = printJob.pageWidth;
//                	object.height = printJob.pageHeight;
				}
//				if(body != null){
//					Application.application.addChild(body);
//					printJob.addObject(body);
//					body.width = printJob.pageWidth;
//                	body.height = printJob.pageHeight;
//				}
//				if(footer != null){
//					Application.application.addChild(footer);
//					printJob.addObject(footer);
//					footer.width = printJob.pageWidth;
//                	footer.height = printJob.pageHeight;
//				}                
                // Create a single-page image.
//                thePrintView.showPage(FormPrintView.PAGE_TYPE_MIDDLE);
                                
                // All pages are queued; remove the FormPrintView control to free memory.
//                if(header != null){
//                	header.visible = false;
//					Application.application.removeChild(header);
//				}
//				if(body != null){
//					Application.application.removeChild(body);
//				}
//				if(footer != null){
//					Application.application.removeChild(footer);
//				}

                if(uiComponent != null){
//                	header.visible = false;
//					Application.application.removeChild(object);
				}
				Application.application.setStyle("background-color", backgroundColor);
            }
            // Send the job to the printer.
            printJob.send();
		}
	}	
}